package com.common.util;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailUtils {

	public static final String DEFALUT_ENCODING = "UTF-8";  
  
    public static void sendTextWithHtml(JavaMailSenderImpl sender, String[] tos, String subject, String text) throws Exception {  
     
    	MimeMessage mailMessage = sender.createMimeMessage();  
        initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text);  
        
        sender.send(mailMessage);  
    }  
  
    public static void sendTextWithImg(JavaMailSenderImpl sender, String[] tos, String subject, String text,  
            String imgId, String imgPath) throws MessagingException {  
    	
        MimeMessage mailMessage = sender.createMimeMessage();  
        MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text,  
                true, true, DEFALUT_ENCODING);  
 
        FileSystemResource img = new FileSystemResource(new File(imgPath));  
        messageHelper.addInline(imgId, img);  

        sender.send(mailMessage);  
    }  
  
    public static void sendWithAttament(JavaMailSenderImpl sender, String[] tos, String subject, String text,  
            String AttachName, String filePath) throws MessagingException {  
    	
        MimeMessage mailMessage = sender.createMimeMessage();  
        MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text,  
                true, true, DEFALUT_ENCODING);  
  
        FileSystemResource file = new FileSystemResource(new File(filePath));
        messageHelper.addAttachment(AttachName, file);  
        
        sender.send(mailMessage);  
  
    }  
  
    public static void sendWithAll(JavaMailSenderImpl sender, String[] tos, String from, String subject, String text,  
            String imgId, String imgPath, String AttachName, String filePath) throws MessagingException { 
    	
        MimeMessage mailMessage = sender.createMimeMessage();  
        MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text,  
                true, true, DEFALUT_ENCODING);  

        FileSystemResource img = new FileSystemResource(new File(imgPath));  
        messageHelper.addInline(imgId, img);  

        FileSystemResource file = new FileSystemResource(new File(filePath));  
        messageHelper.addAttachment(AttachName, file);  

        sender.send(mailMessage);  
  
    }  
  
    private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,  
            String subject, String text) throws MessagingException {  
    	
        return initMimeMessageHelper(mailMessage, tos, from, subject, text, true, false, DEFALUT_ENCODING);  
    }  
  
      
    private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,  
            String subject, String text, boolean isHTML, boolean multipart, String encoding) throws MessagingException {  
    	
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, multipart, encoding);  
        messageHelper.setTo(tos);  
        messageHelper.setFrom(from);  
        messageHelper.setSubject(subject);  
        
        messageHelper.setText(text, isHTML);  
  
        return messageHelper;  
    }   
}
